/*     */ package com.zimbra.cs.mailbox;
/*     */ 
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.ShareLocator;
/*     */ import com.zimbra.cs.session.PendingModifications;
/*     */ import com.zimbra.cs.session.PendingModifications.Change;
/*     */ import com.zimbra.cs.util.Zimbra;
/*     */ import java.util.Collections;
/*     */ import java.util.HashMap;
/*     */ import java.util.LinkedHashMap;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import java.util.Timer;
/*     */ import java.util.TimerTask;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ShareStartStopListener
/*     */   extends MailboxListener
/*     */ {
/*  33 */   private static final Set<MailItem.Type> registeredTypes = Collections.singleton(MailItem.Type.FOLDER);
/*     */   
/*     */   public Set<MailItem.Type> registerForItemTypes()
/*     */   {
/*  37 */     return registeredTypes;
/*     */   }
/*     */   
/*     */   public void notify(MailboxListener.ChangeNotification notification)
/*     */   {
/*  42 */     if (notification.mods.created != null)
/*     */     {
/*  44 */       for (MailItem created : notification.mods.created.values()) {
/*  45 */         if ((created instanceof Folder)) {
/*  46 */           Folder folder = (Folder)created;
/*  47 */           if (folder.getACL() != null) {
/*  48 */             startShare(folder);
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*  53 */     if (notification.mods.modified != null)
/*     */     {
/*  55 */       for (PendingModifications.Change change : notification.mods.modified.values()) {
/*  56 */         if (((change.why & 0x200000) != 0) && ((change.preModifyObj instanceof Folder)) && ((change.what instanceof Folder)))
/*     */         {
/*  58 */           Folder before = (Folder)change.preModifyObj;
/*  59 */           Folder after = (Folder)change.what;
/*  60 */           boolean beforeHasACL = before.getACL() != null;
/*  61 */           boolean afterHasACL = after.getACL() != null;
/*  62 */           if ((!beforeHasACL) && (afterHasACL)) {
/*  63 */             startShare(after);
/*  64 */           } else if ((beforeHasACL) && (!afterHasACL)) {
/*  65 */             stopShare(after);
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*  73 */     if (notification.mods.deleted != null)
/*     */     {
/*  75 */       for (PendingModifications.Change change : notification.mods.deleted.values()) {
/*  76 */         Folder folder = null;
/*  77 */         if ((change.what instanceof Folder)) {
/*  78 */           folder = (Folder)change.what;
/*  79 */         } else if ((change.preModifyObj instanceof Folder)) {
/*  80 */           folder = (Folder)change.preModifyObj;
/*     */         }
/*  82 */         if ((folder != null) && (folder.getACL() != null)) {
/*  83 */           stopShare(folder);
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private void startShare(Folder folder)
/*     */   {
/*  91 */     final String me = folder.getMailbox().getAccountId();
/*  92 */     final String uuid = folder.getUuid();
/*  93 */     TimerTask t = new TimerTask()
/*     */     {
/*     */       public void run() {
/*     */         try {
/*  97 */           Provisioning prov = Provisioning.getInstance();
/*  98 */           ShareLocator shloc = prov.getShareLocatorById(uuid);
/*  99 */           if (shloc == null) {
/* 100 */             prov.createShareLocator(uuid, me);
/*     */           }
/*     */           else {
/* 103 */             prov.reload(shloc, true);
/*     */             
/* 105 */             if (!me.equalsIgnoreCase(shloc.getShareOwnerAccountId()))
/*     */             {
/* 107 */               Map<String, Object> attrs = new HashMap();
/* 108 */               attrs.put("zimbraShareOwnerAccountId", me);
/* 109 */               prov.modifyAttrs(shloc, attrs);
/*     */             }
/*     */           }
/*     */         } catch (Throwable t) {
/* 113 */           ZimbraLog.share.warn("error while processing share start notification", t);
/*     */         }
/*     */       }
/* 116 */     };
/* 117 */     Zimbra.sTimer.schedule(t, 0L);
/*     */   }
/*     */   
/*     */   private void stopShare(Folder folder)
/*     */   {
/* 122 */     final String me = folder.getMailbox().getAccountId();
/* 123 */     final String uuid = folder.getUuid();
/* 124 */     TimerTask t = new TimerTask()
/*     */     {
/*     */       public void run() {
/*     */         try {
/* 128 */           Provisioning prov = Provisioning.getInstance();
/* 129 */           ShareLocator shloc = prov.getShareLocatorById(uuid);
/* 130 */           if (shloc != null)
/*     */           {
/* 132 */             prov.reload(shloc, true);
/*     */             
/* 134 */             if (me.equalsIgnoreCase(shloc.getShareOwnerAccountId())) {
/* 135 */               prov.deleteShareLocator(uuid);
/*     */             }
/*     */           }
/*     */         } catch (Throwable t) {
/* 139 */           ZimbraLog.share.warn("error while processing share stop notification", t);
/*     */         }
/*     */       }
/* 142 */     };
/* 143 */     Zimbra.sTimer.schedule(t, 0L);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/ShareStartStopListener.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */